<!-- 公司信息 正文开始 -->
<div class="layui-fluid" id="mainCompany">
    <div class="layui-card">
        <div class="layui-card-header">筛选条件</div>
        <div class="layui-card-body">
            <form class="layui-form layui-form-pane">
                <div class="layui-row layui-col-space18">
                    <div class="layui-col-md2 layui-col-sm12 layui-col-xs12">
                        <label class="layui-form-label">公司名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="companyName" autocomplete="off" placeholder="请输入公司名称"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm12 layui-col-xs12">
                        <label class="layui-form-label">负责人姓名</label>
                        <div class="layui-input-block">
                            <input type="text" name="legalPersonName" autocomplete="off" placeholder="请输入负责人姓名"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md3 layui-col-sm12 layui-col-xs12">
                        <label class="layui-form-label" style="width: 140px">负责人联系号码</label>
                        <div class="layui-input-block" style="margin-left: 140px">
                            <input type="text" name="legalPersonTel" autocomplete="off" placeholder="请输入负责人联系号码"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm12 layui-col-xs12">
                        <button class="layui-btn layui-btn-fluid" lay-submit
                                id="querySubmitmainCompany" lay-filter="querySubmitmainCompany">筛选
                        </button>
                    </div>
                    <div class="layui-col-md1 layui-col-sm12 layui-col-xs12">
                        <button type="reset" class="layui-btn layui-btn-primary layui-btn-fluid">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="layui-card">
        <table id="listMainCompany" lay-filter="listMainCompany"></table>
    </div>
</div>

<!-- 表格批量工具栏 -->
<script type="text/html" id="mainCompanyToolbar">
    <div class="layui-btn-container">
        {{# if(layui.admin.hasPerm(layui.reqApi.admin.mainCompany.createOrUpdate.getPermissionUrl("POST"))){ }}
        <a class="layui-btn layui-btn-sm" lay-event="mainCompanyToolbarSave">
            <i class="layui-icon">&#xe61f;</i>添加公司信息
        </a>
        {{# }if(layui.admin.hasPerm(layui.reqApi.admin.mainCompany.delete.getPermissionUrl("POST"))){ }}
        <a class="layui-btn layui-btn-danger layui-btn-sm" lay-event="mainCompanyToolbarDel">
            <i class="layui-icon">&#xe640;</i>批量删除</a>
        </a>
        {{# } }}
    </div>
</script>

<!-- 表格操作模板 -->
<script type="text/html" id="mainCompanyTablebar">
    {{# if(layui.admin.hasPerm(layui.reqApi.admin.mainCompany.createOrUpdate.getPermissionUrl("POST"))){ }}
    <a class="layui-btn layui-btn-xs" lay-event="mainCompanyTablebarUpdate"> 编辑 </a>
    {{# } if(layui.admin.hasPerm(layui.reqApi.admin.mainCompany.delete.getPermissionUrl("POST"))){ }}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="mainCompanyTablebarDel"> 删除 </a>
    {{# } }}
</script>

<!-- 新增或者修改 -->
<script type="text/html" id="saveAndUpdateCompanyContainer">
    <form lay-filter="saveAndUpdateCompanyForm" class="layui-form layui-form-pane">
        <input name="id" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label red-star-label">公司名称</label>
            <div class="layui-input-block">
                <input name="companyName" placeholder="请输入公司名称" type="text" class="layui-input" maxlength="64"
                       lay-verType="tips" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label red-star-label">负责人姓名</label>
            <div class="layui-input-block">
                <input name="legalPersonName" placeholder="请输入负责人姓名" type="text" class="layui-input" maxlength="64"
                       lay-verType="tips" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label red-star-label">负责人联系号码</label>
            <div class="layui-input-block">
                <input name="legalPersonTel" placeholder="请输入联系号码" type="text" class="layui-input" maxlength="64"
                       lay-verType="tips" lay-verify="required"/>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
            <button class="layui-btn" lay-filter="saveAndUpdateCompanySubmit" lay-submit>保存</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script>
    layui.use(['layer', 'table', 'admin', 'setter', 'form', 'reqApi', 'reqEnum', 'promiseReq', 'formSelects'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var table = layui.table;
        var admin = layui.admin;
        var setter = layui.setter;
        var form = layui.form;
        var reqApi = layui.reqApi;
        var reqEnum = layui.reqEnum;
        var promiseReq = layui.promiseReq;
        var formSelects = layui.formSelects;

        // 引用对象
        var main = 'mainCompany',
            list = 'listMainCompany',
            querySubmit = 'querySubmitmainCompany',
            mainCompanyToolbar = 'mainCompanyToolbar',
            mainCompanyTablebar = 'mainCompanyTablebar',
            saveAndUpdateCompany = 'saveAndUpdateCompany',
            saveAndUpdateCompanyContainer = 'saveAndUpdateCompanyContainer',
            saveAndUpdateCompanySubmit = 'saveAndUpdateCompanySubmit',
            saveAndUpdateCompanyForm = 'saveAndUpdateCompanyForm'
        ;

        // 等待框
        admin.showLoading('#' + main, 1, '.5');

        // 筛选
        form.on('submit(' + querySubmit + ')', function (obj) {
            admin.showLoading('#' + main, 1, '.5');
            listRender(obj.field);
            return false;
        });

        // 表格筛选触发
        function listQuery() {
            $('#' + querySubmit).click();
        }

        // 执行
        listRender();

        // 表格渲染
        function listRender(param) {
            // 权限设置
            var toolbar = '#' + mainCompanyToolbar;
            var handle = [{title: '操作', align: "center",templet: '#'+ mainCompanyTablebar, width: 180, fixed: 'left'}];
            if (!admin.hasPerm(layui.reqApi.admin.mainCompany.createOrUpdate.getPermissionUrl("POST"))
                &&  !admin.hasPerm(layui.reqApi.admin.mainCompany.delete.getPermissionUrl("POST"))) {
                toolbar = false;
            }

            if (!admin.hasPerm(layui.reqApi.admin.mainCompany.createOrUpdate.getPermissionUrl("POST"))
                &&  !admin.hasPerm(layui.reqApi.admin.mainCompany.delete.getPermissionUrl("POST"))) {
                handle = []
            }

            // 初始化
            table.render({
                elem: '#' + list
                , height: 'full-300'
                , toolbar: toolbar
                , headers: {Authorization: setter.getBearerToken()}
                , url: reqApi.admin.mainCompany.list
                , cellMinWidth: 150
                , cols: [[].concat(handle).concat([
                    {title: '公司id', field: 'id', align: 'left', hide:true},
                    {title: '公司名称', field: 'companyName', align: 'left'},
                    {title: '负责人姓名', field: 'legalPersonName', align: 'left'},
                    {title: '负责人联系号码', field: 'legalPersonTel', align: 'left'},
                    {title: '创建时间', field: 'createTime', align: 'center'},
                    {title: '更新时间', field: 'updateTime', align: 'center'},
                ])]
                , where: param
                , done: function () {
                    Promise.all([null]).finally(function () {
                        form.render();
                        admin.removeLoading('#' + main);
                    });
                }
                , request: {pageName: 'cursor', limitName: 'limit'}
                , parseData: function (rs) {
                    return {"code": rs.code, "msg": rs.msg, "count": rs.data.total, "data": rs.data.records};
                }
                , response: {statusCode: 200}
                , defaultToolbar: []
                , page: true
                , limit: 50
                , loading: true
                , text: {none: '暂无相关数据'}
            });
        }

        //监听表格批量工具栏
        table.on('toolbar(' + list + ')', function (obj) {
            switch (obj.event) {
                case 'mainCompanyToolbarSave':
                    suRender();
                    break;
                case 'mainCompanyToolbarDel':
                    var data = table.checkStatus(list).data;
                    if (0 === data.length) {
                        layer.msg('请勾选要删除的数据', {time: 1000});
                        break;
                    }
                    var idList = [];
                    for (var i in data) {
                        idList[i] = data[i].id;
                    }
                    delRender(idList);
                    break;
            }
        });

        // 监听表格操作行
        table.on('tool(' + list + ')', function (obj) {
            var id = obj.data.id;
            switch (obj.event) {
                case 'mainCompanyTablebarUpdate':
                    suRender(id);
                    break;
                case 'mainCompanyTablebarDel':
                    delRender([id]);
                    break;
            }
        });

        // 新增或者修改
        function suRender(id) {
            admin.open({
                id: saveAndUpdateCompany
                , type: 1
                , title: id==null ? '新增':'修改'
                , shadeClose: true
                , content: $('#' + saveAndUpdateCompanyContainer).html()
                , success: function (layero, index) {
                    $(layero).children('.layui-layer-content').css('overflow', 'visible');
                    admin.showLoading('#' + saveAndUpdateCompany, 1, '.5');
                    suSubmitRender(index);

                    if (id) {
                        var infoReq = promiseReq.req({
                            url: reqApi.admin.mainCompany.get.restfulFormat({id: id})
                            , success: function (rs) {
                                var data = rs.data;
                                form.val(saveAndUpdateCompanyForm, {
                                    "id": id,
                                    "companyName": data.companyName,
                                    "legalPersonName": data.legalPersonName,
                                    "legalPersonTel": data.legalPersonTel
                                });
                            }
                        });

                        Promise.all([null]).finally(function () {
                            admin.removeLoading('#' + saveAndUpdateCompany);
                            form.render();
                        });
                    } else {
                        Promise.all([null]).finally(function () {
                            admin.removeLoading('#' + saveAndUpdateCompany);
                            form.render();
                        });
                    }
                }
            });
        }

        // 初始化新增或者修改提交
        function suSubmitRender(index) {
            form.on('submit(' + saveAndUpdateCompanySubmit + ')', function (obj) {
                var reqEL = layer.msg('操作执行中', {icon: 16, shade: 0.01, time: 6000});
                admin.req({
                    url: reqApi.admin.mainCompany.createOrUpdate
                    , type: 'post'
                    , data: obj.field
                    , complete: function () {
                        layer.close(reqEL);
                    }
                    , success: function () {
                        layer.msg('操作成功', {offset: '15px', icon: 1, time: 500});
                        layer.close(index);
                        listQuery();
                    }
                });
                return false;
            });
        }

        // 删除操作
        function delRender(idList) {
            layer.confirm('确定删除所选信息？', {
                skin: 'layui-layer-admin'
                , btn: ['确定', '取消']
            }, function () {
                var reqEL = layer.msg('操作执行中', {icon: 16, shade: 0.01, time: 6000});
                admin.req({
                    url: reqApi.admin.mainCompany.delete
                    , type: 'post'
                    , data: idList
                    , complete: function () {
                        layer.close(reqEL);
                    }
                    , success: function () {
                        layer.msg('操作成功', {offset: '15px', icon: 1, time: 500});
                        listQuery();
                    }
                });
            }, function () {
            });
        }
    });
</script>